<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <link rel="stylesheet" href="/${BasePath}/static/mobile/css/ydui.css" />
    <link rel="stylesheet" href="/${BasePath}/static/mobile/css/base.css" />
    <link rel="stylesheet" href="/${BasePath}/static/mobile/css/style.css" />
    <link rel="stylesheet" href="/${BasePath}/static/mobile/css/swiper.min.css" />  
    <title>半岛云</title>  
</head>
<body>
    <input type="hidden" id="active" value="gwc">
	<div class="wrap">
		<!--标题栏-->
		<div class="fixed_top">
			<div class="top-l"><a href="javascript:window.history.back()"><i class="icons">&#xe626;</i></a></div>
			<h1>购物车</h1>
			<div class="top-r"><a href="javascript:;" class="bjbtn">编辑</a></div>
		</div>
       <#if car?? && (car?size > 0) >
		<!--主体区域-->
		<div class="container" style="padding-bottom:110px">	
			
	        <ul class="gw_list">
	        	<li>
	        	  <#list car as car>
	        		<dl>
	        			<dt><i class="icons unchecked">&#xe619;</i><i class="icons checked">&#xe607;</i><span>${car.shopName!""}</span></dt>
	        			<#list car.shopCart as shopCart>
	        			<dd>			    
	        			    <input type="hidden" class="ids" value="${shopCart.id!''}">
	        			    <input type="hidden" class="tempShopId" value="${car.id!''}">
	        				<div class="gwinfo">
		        				<div class="checks">
		        					<i class="icons unchecked">&#xe619;</i><i class="icons checked">&#xe607;</i>
		        				</div>
		        				<img class="gw_img" src="${shopCart.productCover!''}"/>
		        				<h4><p>${shopCart.productName!""}</p></h4>
		        				<p><span class="price">￥${shopCart.productPrice!""}</span></p>
		        				<div class="addminus">
		        					<a href="javsacript:;" class="minus"><i class="icons">&#xe67d;</i></a>
		        					<span class="number">${shopCart.productNum!""}</span>
		        					<a href="javsacript:;" class="add" data-id="${shopCart.id!''}"><i class="icons">&#xe60b;</i></a>
		        				</div>
	        				</div>
	        			</dd>
	        			</#list>
	        		</dl>
				</#list>
	        	</li>
	        </ul>
		</div>
	    
	    
	   <!--结算--> 
	   <div class="pay">
	   	   <label><i class="icons unchecked">&#xe619;</i><i class="icons checked">&#xe607;</i>全选</label>
	   	   <div class="js paytab">
	   	   	   <span class="yf">不含运费</span>
		   	   <p>合计:<span>￥0</span></p>
		   	   <a href="javascript:;" class="qrdd">确认订单(<b>0</b>)</a>
	   	   </div>
	   	   <div class="bj paytab">
		   	   <a href="javascript:;" class="sc">删除</a>
		   	   <!--<a href="javascript:;" class="yzsc">移至收藏</a>-->
	   	   </div>
	   </div>
       <#else>
	     <div class="null">
	     	<div>
	     		<a ><i class="icons" style="font-size: 150px;color:#17CE70;">&#xe776;</i><p>还没有相关数据呦</p></a>
	     	</div>
		 </div>
	 </#if>	  

	  
		<!--导航栏-->
        <#include "/common/foot.html">
       
	</div>
</body>
<script type="text/javascript" src="/${BasePath}/static/mobile/js/ydui.flexible.js"></script>
<script type="text/javascript" src="/${BasePath}/static/mobile/js/ydui.js"></script>
<script type="text/javascript">
    $(function(){
    	//单个商品
	    $("body").on("click","dd .checks",function(){
	    	$(this).toggleClass("active");
	    	sum()
	    })

	    //单个店铺商品
	    $("body").on("click","dt",function(){
	    	$(this).toggleClass("active");
	    	if($(this).hasClass("active")){
                $(this).parent().find("dd").find(".checks").addClass("active")
	    	}else{
	    		$(this).parent().find("dd").find(".checks").removeClass("active")
	    	}
	    	sum()
	    })
	    
	    //加
	    $("body").on("click",".add",function(){
	    	$(this).prev().text($(this).prev().text()*1+1);
	    	sum();
	    	chooseCartNum($(this).attr('data-id'),"add");
	    })
	    
	    //减
	    $("body").on("click",".minus",function(){
	    	if($(this).next().text()*1 > 1){
	    		$(this).next().text($(this).next().text()*1-1)
	    		chooseCartNum($(this).attr('data-id'),"minus");
	    	}else{
	    		YDUI.dialog.alert('再减就没有拉！');
	    	}
	    	sum();
	    })
	    
	    //全选
	    $("body").on("click",".pay label",function(){
	    	$(this).toggleClass("active");
	    	if($(this).hasClass("active")){
	    		$(".checks,dt").addClass("active")
	    	}else{
	    		$(".checks,dt").removeClass("active")
	    	}
	    	sum()	
	    })
	    
	    //统计
	    function totals(){
	    	var num=0;
	    	$(".checks.active").each(function(index,item){
	    		num += ($(item).parents("dd").find(".price").text().substring(1,$(item).parents("dd").find(".price").text().length)*$(item).parents("dd").find(".number").text());
	    	})
	    	return num
	    }
	    function sum(){
	    	$(".pay b").text($(".checks.active").size());
	    	$(".pay p span").text("￥"+totals())
	    }
	    
	    
	    function chooseCartNum(cartId,type){
            $.ajax({
                cache: true,
                type: "POST",
                url: "/${BasePath}/order/chooseCartNum",
                data: {"cartId":cartId,"type":type},
                async: false,
                error: function(request) {
                	YDUI.dialog.alert('系统错误，请联系管理员！');
                }
            });
	    }
	    //编辑
	    var flag=true;
	    $(".bjbtn").click(function(){
	    	if(flag){
	    	  $(this).text("完成");
	    	  $(".js").hide().next().show();
	    	}else{
	    	  $(this).text("编辑");
	    	  $(".js").show().next().hide();
	    	}
	    	flag=!flag
	    })
	    //删除
	    $(".sc").click(function(){
	    	YDUI.dialog.confirm('提示',  '确定删除！',  function () {
	            var cartId=new Array();
		    	$(".checks.active").each(function(index,item){
		    		var num = ($(item).parents("dd").find(".ids").val());
		    		cartId.push(num);
		    	})
		    	
	            $.ajax({
	                cache: true,
	                type: "POST",
	                url: "/${BasePath}/order/deleteCart",
	                data: {"cartId":cartId},
	                async: false,
	                error: function(request) {
	                	YDUI.dialog.alert('系统错误，请联系管理员！');
	                },
	                success: function(data) {
	                	if(data.code=="1"){
	                		YDUI.dialog.alert(data.msg, function(){
	                			location.reload();
	                		});
	                	}
	                	
	                }
	            });
	    	});
	    })	    

	    //结算
	    $(".qrdd").click(function(){
            var cartId=new Array();
            var tempShopId=new Array();
	    	$(".checks.active").each(function(index,item){
	    		var num = ($(item).parents("dd").find(".ids").val());
	    		var temp = ($(item).parents("dd").find(".tempShopId").val());
	    		cartId.push(num);
	    		tempShopId.push(temp);
	    	})	
	    	tempShopId = unique1(tempShopId);
	    	if(tempShopId.length!=1){
	    		YDUI.dialog.alert('目前仅支持结算一个店面的订单，请选择单个店铺的商品！');
	    		return
	    	}
			var form = $("<form>");
 		    form.attr("style","display:none");
 		    form.attr("target","");
 		    form.attr("method","post");
 		    //请求地址
 		    form.attr("action","/${BasePath}/order/orderConfirm");
 		    var input1 = $("<input>");
 		    input1.attr("type","hidden");
 		    input1.attr("name","cartId");
 		    input1.attr("value",cartId);
 		    $("body").append(form);
 		    form.append(input1);
 		    form.submit();
 		    form.remove();   
	    })	
	    
	    function unique1(arr){
	    	  var hash=[];
	    	  for (var i = 0; i < arr.length; i++) {
	    	     if(hash.indexOf(arr[i])==-1){
	    	      hash.push(arr[i]);
	    	     }
	    	  }
	    	  return hash;
	    }
	})
</script>
</html>